<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 3.9.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=7.4.2">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=7.4.2">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=7.4.2">
  <link rel="mask-icon" href="/images/logo.svg?v=7.4.2" color="#222">

<link rel="stylesheet" href="/css/main.css?v=7.4.2">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.7.0">


<script id="hexo-configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '7.4.2',
    exturl: false,
    sidebar: {"position":"left","display":"post","offset":12,"onmobile":false},
    copycode: {"enable":false,"show_result":false,"style":null},
    back2top: {"enable":true,"sidebar":false,"scrollpercent":false},
    bookmark: {"enable":false,"color":"#222","save":"auto"},
    fancybox: false,
    mediumzoom: false,
    lazyload: false,
    pangu: false,
    algolia: {
      appID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    },
    localsearch: {"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},
    path: '',
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    translation: {
      copy_button: '复制',
      copy_success: '复制成功',
      copy_failure: '复制失败'
    },
    sidebarPadding: 40
  };
</script>

  <meta name="description" content="通过对Docker概念的了解，1个容器就相当于一套完整的应用运行环境，没有任何外部依赖。同时，能够进行快速的创建与销毁，实现服务的快速构建、组合与调度。最近的项目，因公司服务器的安全性设置，服务器拒绝访问外部网络，加之应用运行时需要的依赖软件较多，且配置繁琐。故此，就使用docker对应用服务进行快速构建，并将初始稳定版本备份，上传至公司服务器即可快速启动应用。接下来就说说docker的安装与使用">
<meta name="keywords" content="Docker">
<meta property="og:type" content="article">
<meta property="og:title" content="Docker的安装与使用">
<meta property="og:url" content="http://chengg.top/2017/05/10/Docker的安装与使用/index.html">
<meta property="og:site_name" content="CHENG&#39;s NOTES">
<meta property="og:description" content="通过对Docker概念的了解，1个容器就相当于一套完整的应用运行环境，没有任何外部依赖。同时，能够进行快速的创建与销毁，实现服务的快速构建、组合与调度。最近的项目，因公司服务器的安全性设置，服务器拒绝访问外部网络，加之应用运行时需要的依赖软件较多，且配置繁琐。故此，就使用docker对应用服务进行快速构建，并将初始稳定版本备份，上传至公司服务器即可快速启动应用。接下来就说说docker的安装与使用">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2019-10-23T14:34:12.034Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Docker的安装与使用">
<meta name="twitter:description" content="通过对Docker概念的了解，1个容器就相当于一套完整的应用运行环境，没有任何外部依赖。同时，能够进行快速的创建与销毁，实现服务的快速构建、组合与调度。最近的项目，因公司服务器的安全性设置，服务器拒绝访问外部网络，加之应用运行时需要的依赖软件较多，且配置繁琐。故此，就使用docker对应用服务进行快速构建，并将初始稳定版本备份，上传至公司服务器即可快速启动应用。接下来就说说docker的安装与使用">

<link rel="canonical" href="http://chengg.top/2017/05/10/Docker的安装与使用/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome: false,
    isPost: true,
    isPage: false,
    isArchive: false
  };
</script>

  <title>Docker的安装与使用 | CHENG's NOTES</title>
  








  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-meta">

    <div>
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">CHENG's NOTES</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
  </div>

  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
        <li class="menu-item menu-item-music">

    <a href="/music/" rel="section"><i class="fa fa-fw fa-music"></i>歌单</a>

  </li>
        <li class="menu-item menu-item-movies">

    <a href="/movies" rel="section"><i class="fa fa-fw fa-video-camera"></i>影视</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-fw fa-user"></i>关于</a>

  </li>
  </ul>

</nav>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content">
            

  <div class="posts-expand">
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block " lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://chengg.top/2017/05/10/Docker的安装与使用/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="https://tvax4.sinaimg.cn/crop.0.0.480.480.180/006ARivqly8fuwtj1v9edj30dc0dct9q.jpg">
      <meta itemprop="name" content="一叶不知秋丶">
      <meta itemprop="description" content="有人天生为王，有人落草为寇。">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="CHENG's NOTES">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Docker的安装与使用
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2017-05-10 15:21:39" itemprop="dateCreated datePublished" datetime="2017-05-10T15:21:39+08:00">2017-05-10</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Docker/" itemprop="url" rel="index">
                    <span itemprop="name">Docker</span>
                  </a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>通过对Docker概念的了解，1个容器就相当于一套完整的应用运行环境，没有任何外部依赖。同时，能够进行快速的创建与销毁，实现服务的快速构建、组合与调度。最近的项目，因公司服务器的安全性设置，服务器拒绝访问外部网络，加之应用运行时需要的依赖软件较多，且配置繁琐。故此，就使用docker对应用服务进行快速构建，并将初始稳定版本备份，上传至公司服务器即可快速启动应用。接下来就说说docker的安装与使用，以CentOS为例。</p>
<a id="more"></a> 
<h3 id="安装与启动"><a href="#安装与启动" class="headerlink" title="安装与启动"></a>安装与启动</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">#系统升级</span><br><span class="line">yum -y update  </span><br><span class="line">#添加yum仓库</span><br><span class="line">cat &gt;/etc/yum.repos.d/docker.repo &lt;&lt;-EOF  </span><br><span class="line">[dockerrepo]  </span><br><span class="line">name=Docker Repository  </span><br><span class="line">baseurl=https://yum.dockerproject.org/repo/main/centos/7  </span><br><span class="line">enabled=1  </span><br><span class="line">gpgcheck=1  </span><br><span class="line">gpgkey=https://yum.dockerproject.org/gpg  </span><br><span class="line">EOF</span><br><span class="line">#安装</span><br><span class="line">yum -y  install docker  </span><br><span class="line">#镜像加速</span><br><span class="line">cat &gt;/etc/docker/daemon.json &lt;&lt;-EOF  </span><br><span class="line">&#123;  </span><br><span class="line">  &quot;registry-mirrors&quot;: [&quot;https://docker.mirrors.ustc.edu.cn&quot;]  </span><br><span class="line">&#125;  </span><br><span class="line">EOF </span><br><span class="line">#启动</span><br><span class="line">systemctl start docker.service</span><br></pre></td></tr></table></figure>
<h3 id="常用docker命令"><a href="#常用docker命令" class="headerlink" title="常用docker命令"></a>常用docker命令</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line">#例如，搜索并拉取镜像centos</span><br><span class="line">docker search  centos 或去https://hub.docker.com/ 搜索  </span><br><span class="line">docker pull centos  </span><br><span class="line">#运行镜像命令示例</span><br><span class="line">docker run -itd -p 12346:8080 --name=test centos:latest </span><br><span class="line">docker run参数说明见：</span><br><span class="line">https://docs.docker.com/engine/reference/commandline/run/#parent-command</span><br><span class="line"></span><br><span class="line">#镜像和容器相关操作</span><br><span class="line">docker start [容器id]    #启动容器</span><br><span class="line">docker stop [容器id]     #停止容器</span><br><span class="line">docker restart [容器id]  #重启容器</span><br><span class="line">docker rm [容器id]       #删除容器</span><br><span class="line">docker rmi [镜像id]      #删除镜像</span><br><span class="line">docker ps -a  #查看所有容器</span><br><span class="line">#进入容器：</span><br><span class="line">docker attach [容器id]   #不推荐此方式</span><br><span class="line">docker exec -it [容器id] /bin/bash</span><br><span class="line"></span><br><span class="line">启动所有的容器：  </span><br><span class="line">docker start $(docker ps -a | awk &apos;&#123; print $1&#125;&apos; | tail -n +2)  </span><br><span class="line">关闭所有的容器：  </span><br><span class="line">docker stop $(docker ps -a | awk &apos;&#123; print $1&#125;&apos; | tail -n +2)  </span><br><span class="line">删除所有的容器：  </span><br><span class="line">docker rm $(docker ps -a | awk &apos;&#123; print $1&#125;&apos; | tail -n +2)  </span><br><span class="line">删除所有的镜像：  </span><br><span class="line">docker rmi $(docker images | awk &apos;&#123;print $3&#125;&apos; |tail -n +2)  </span><br><span class="line"></span><br><span class="line">#备份与迁移</span><br><span class="line">docker commit -a &quot;author&quot; -m &quot;创建容器快照&quot; [容器id]  backup  #创建快照</span><br><span class="line">docker save -o  /home/zwzf/backup.tar backup   #导出tar</span><br><span class="line">docker load -i ~/backup.tar #加载tar</span><br><span class="line"></span><br><span class="line">#容器与宿主机数据交互</span><br><span class="line">docker cp host_path [容器id]:/container_path   #宿主机到容器</span><br><span class="line">docker cp [容器id]:/container_path host_path   #容器到宿主机</span><br><span class="line">scp -r container_path root@192.168.20.99:host_path   #容器到宿主机-scp命令</span><br><span class="line"></span><br><span class="line">#查看容器信息</span><br><span class="line">docker inspect [容器id]</span><br></pre></td></tr></table></figure>
<h3 id="DockerFile示例"><a href="#DockerFile示例" class="headerlink" title="DockerFile示例"></a>DockerFile示例</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">FROM centos:latest  </span><br><span class="line">MAINTAINER author_name&lt;dev@email.com.cn&gt;  </span><br><span class="line"># now add java and tomcat support in the container  </span><br><span class="line">ADD jdk-8u151-linux-x64.tar.gz /usr/local/  </span><br><span class="line">ADD apache-tomcat-8.5.24.tar.gz /usr/local/  </span><br><span class="line">COPY entrypoint.sh /  </span><br><span class="line"># configuration of java and tomcat ENV  </span><br><span class="line">ENV JAVA_HOME /usr/local/jdk1.8.0_151  </span><br><span class="line">ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar  </span><br><span class="line">ENV CATALINA_HOME /usr/local/apache-tomcat-8.5.24  </span><br><span class="line">ENV CATALINA_BASE /usr/local/apache-tomcat-8.5.24  </span><br><span class="line">ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin  </span><br><span class="line"># container listener port   </span><br><span class="line">EXPOSE 8080   </span><br><span class="line">WORKDIR /usr/local/apache-tomcat-8.5.24/bin  </span><br><span class="line"># startup web application services by self   </span><br><span class="line">CMD startup.sh run &amp;&amp; tail -f /usr/local/apache-tomcat-8.5.24/logs/catalina.out</span><br></pre></td></tr></table></figure>




    
    
    

      <footer class="post-footer">
             <div> 
    <div style="text-align:center;color: #ccc;font-size:14px;"> 
    ------------- 本文结束 感谢您的阅读 ------------- 
    </div> 
    </div>
    </div>
          <div class="post-tags">
              <a href="/tags/Docker/" rel="tag"># Docker</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-next post-nav-item">
                <a href="/2017/05/01/Docker基本概念初探/" rel="next" title="Docker基本概念初探">
                  <i class="fa fa-chevron-left"></i> Docker基本概念初探
                </a>
            </div>

            <span class="post-nav-divider"></span>

            <div class="post-nav-prev post-nav-item">
            </div>
          </div>
      </footer>
    
  </article>
  
  
  

  </div>


          </div>
          

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#安装与启动"><span class="nav-number">1.</span> <span class="nav-text">安装与启动</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#常用docker命令"><span class="nav-number">2.</span> <span class="nav-text">常用docker命令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#DockerFile示例"><span class="nav-number">3.</span> <span class="nav-text">DockerFile示例</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <img class="site-author-image" itemprop="image" alt="一叶不知秋丶"
    src="https://tvax4.sinaimg.cn/crop.0.0.480.480.180/006ARivqly8fuwtj1v9edj30dc0dct9q.jpg">
  <p class="site-author-name" itemprop="name">一叶不知秋丶</p>
  <div class="site-description" itemprop="description">有人天生为王，有人落草为寇。</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">5</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">2</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">4</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2019 </span>
 
  <span class="author" itemprop="copyrightHolder">Hosted by Gitee/Coding Pages 一叶不知秋丶</span>
</div>

        












        
      </div>
    </footer>
  </div>

  
  <script src="/lib/jquery/index.js?v=2.1.3"></script>
  <script src="/lib/anime.min.js?v=3.1.0"></script>
  <script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  <script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
<script src="/js/utils.js?v=7.4.2"></script><script src="/js/motion.js?v=7.4.2"></script>
<script src="/js/schemes/pisces.js?v=7.4.2"></script>
<script src="/js/next-boot.js?v=7.4.2"></script>

  <script defer src="/lib/three/three.min.js"></script>
    <script defer src="/lib/three/canvas_lines.min.js"></script>


  


















  

  

  

</body>
</html>
